﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.Collections.Generic;


namespace NJCORS.forms
{
    public partial class UploadSuspectPoint : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            diverrormsg.Style.Add("display", "none");
        }

        protected void btnadd_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                if (txbB.Text.Length < 1)
                {
                    throw new Exception("请填写可疑点纬度B！");
                }
                if (txbL.Text.Length < 1)
                {
                    throw new Exception("请填写可疑点经度L！");
                }
                BLL.SuspectPointInfo bspi = new BLL.SuspectPointInfo();
                Model.SuspectPointInfo mspi = new Model.SuspectPointInfo();
                mspi.Comments1 = txbComments.Text.Trim();
                mspi.Comments2 = "";
                mspi.Enabled = true;
                mspi.Handled = false;
                mspi.HandleRecordID = -1;
                mspi.HandleTime = new DateTime(1900, 1, 1);
                mspi.SuspectPointB = double.Parse(txbB.Text.Trim());
                mspi.SuspectPointL = double.Parse(txbL.Text.Trim());
                int nUserID, nLandID;
                JudgePoint(mspi.SuspectPointB, mspi.SuspectPointL, out nUserID, out nLandID);
                mspi.InspectUserID = nUserID;
                mspi.LandID = nLandID;
                mspi.Materials1 = "";
                mspi.Materials2 = "";

                bspi.Add(mspi);
                ShowMessage(true, "添加可疑点成功！");
            }
            catch (System.Exception ex)
            {
                ShowMessage(false, ex.Message);
            }
        }

        private void JudgePoint(double dbB, double dbL, out int nUserID, out int nLandID)
        {
            nUserID = nLandID = -1;
            BLL.InspectUserInfo biui = new BLL.InspectUserInfo();
            DataSet ds = biui.GetList("");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                LawlessAreaJudge laj = new LawlessAreaJudge(Convert.ToString(ds.Tables[0].Rows[i]["InspectUserScope"]));
                if (laj.run(dbB, dbL))
                {
                    nUserID = Convert.ToInt32(ds.Tables[0].Rows[i]["InspectUserID"]);
                    break;
                }
            }
            BLL.LandInfo bli = new BLL.LandInfo();
            ds = bli.GetList("");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (JudgeLandPoint(Convert.ToInt32(ds.Tables[0].Rows[i]["LandID"]), dbB,dbL,ref nLandID))
                {
                    break;
                }
            }
        }

        private bool JudgeLandPoint(int nID, double dbB, double dbL, ref int nLandID)
        {
            string strScope = "";
            BLL.LandInfo bli = new BLL.LandInfo();
            Model.LandInfo mli = bli.GetModel(nID);
            if (mli.LandScope.Contains(";"))
            {
                string[] strIDs = (mli.LandScope.Substring(mli.LandScope.IndexOf(";") + 1)).Split(',');
                for (int i = 0; i < strIDs.Length; i++)
                {
                    if (JudgeLandPoint(Convert.ToInt32(strIDs[i]), dbB, dbL, ref nLandID))
                    {
                        return true;
                    }
                }
                strScope = mli.LandScope.Substring(0, mli.LandScope.IndexOf(";"));
            }
            else
            {
                strScope = mli.LandScope;
            }
            LawlessAreaJudge laj = new LawlessAreaJudge(strScope);
            if (laj.run(dbB,dbL))
            {
                nLandID = nID;
                return true;
            }
            return false;
        }

        protected void ShowMessage(bool issuccessful, string message)
        {
            diverrormsg.Style.Add("display", "block");
            if (issuccessful == true)
            {
                diverrormsg.Style.Add("background-color", "#CCFF99");
                diverrormsg.Style.Add("border-color", "#009900");
                diverrormsg.Style.Add("color", "#006600");
                diverrormsg.Style.Add("background-image", "url(../themes/images/successful.gif)");
                lberrormsg.Text = message;
            }
            else
            {
                diverrormsg.Style.Add("background-color", "#ffebe8");
                diverrormsg.Style.Add("border-color", "#dd3c10");
                diverrormsg.Style.Add("color", "#dd3c10");
                diverrormsg.Style.Add("background-image", "url(../themes/images/error.gif)");
                lberrormsg.Text = message;
            }
        }

    }
}
